<template>
  <div class="logic-flow-view">
    <h3 class="demo-title">业务流程图</h3>
    <!-- 辅助工具栏 -->
    <Control
      class="demo-control"
      v-if="lf"
      :lf="lf"
      @catData="$_catData"
    ></Control>
    <!-- 节点面板 -->
    <NodePanel v-if="lf" :lf="lf" :nodeList="nodeList"></NodePanel>
    <!-- 画布 -->
    <div id="LF-view"></div>
    <!-- 用户节点自定义操作面板 -->
    <AddPanel
      v-if="showAddPanel"
      class="add-panel"
      :style="addPanelStyle"
      :lf="lf"
      :nodeData="addClickNode"
      @addNodeFinish="hideAddPanel"
    >
    </AddPanel>
    <!-- 属性面板 -->
    <a-drawer
      title="设置节点属性"
      :body-style="{ padding: '20px' }"
      placement="right"
      width="500"
      :visible="dialogVisible"
      @close="closeDialog"
    >
      <PropertyDialog
        v-if="dialogVisible"
        :nodeData="clickNode"
        :lf="lf"
        @setPropertiesFinish="closeDialog"
      ></PropertyDialog>
    </a-drawer>
    <!-- 数据查看面板 -->
    <a-modal title="数据" width="50%" v-model="dataVisible" :footer="null">
      <DataDialog :graphData="graphData"></DataDialog>
    </a-modal>
  </div>
</template>
<script>
import LogicFlow from "@logicflow/core";
import { Menu, Snapshot } from "@logicflow/extension";
import "@logicflow/core/dist/style/index.css";
import "@logicflow/extension/lib/style/index.css";
import NodePanel from "@/flow/NodePanel";
import AddPanel from "@/flow/AddPanel";
import Control from "@/flow/Control";
import PropertyDialog from "@/flow/PropertyDialog";
import DataDialog from "@/flow/DataDialog";
import { nodeList } from "@/flow/config";
import {
  registerStart,
  registerUser,
  registerEnd,
  registerPush,
  registerDownload,
  registerPolyline,
  registerTask,
} from "@/flow/registerNode";
const demoData = require("@/flow/data.json");

export default {
  name: "LF",
  components: { NodePanel, AddPanel, Control, PropertyDialog, DataDialog },
  data() {
    return {
      lf: null,
      showAddPanel: false,
      addPanelStyle: {
        top: 0,
        left: 0,
      },
      nodeData: null,
      addClickNode: null,
      clickNode: null,
      dialogVisible: false,
      graphData: null,
      dataVisible: false,
      config: {
        background: {
          color: "#f7f9ff",
        },
        grid: {
          size: 10,
          visible: true,
        },
        keyboard: {
          enabled: true,
        },
        style: {
          rect: {
            radius: 6,
          },
          edgeText: {
            background: {
              fill: "#fff",
            },
          },
        },
        edgeTextDraggable: true,
        guards: {
          beforeClone(data) {
            console.log("beforeClone", data);
            return true;
          },
          beforeDelete(data) {
            console.log("beforeDelete", data);
            return true;
          },
        },
      },
      moveData: {},
      nodeList,
    };
  },
  mounted() {
    this.$_initLf();
  },
  methods: {
    $_initLf() {
      // 使用插件
      LogicFlow.use(Menu);
      LogicFlow.use(Snapshot);
      const lf = new LogicFlow({
        ...this.config,
        container: document.querySelector("#LF-view"),
      });
      this.lf = lf;
      // 设置主题
      lf.setTheme({
        circle: {
          r: 20,
          stroke: "#000000",
          outlineColor: "#88f",
          strokeWidth: 1,
        },
        rect: {
          outlineColor: "#88f",
          strokeWidth: 1,
        },
        polygon: {
          strokeWidth: 1,
        },
        polyline: {
          stroke: "#000000",
          hoverStroke: "#000000",
          selectedStroke: "#000000",
          outlineColor: "#88f",
          strokeWidth: 1,
        },
        nodeText: {
          color: "#000000",
        },
        edgeText: {
          color: "#000000",
          background: {
            fill: "#f7f9ff",
          },
        },
      });
      this.$_registerNode();
    },
    // 自定义
    $_registerNode() {
      registerStart(this.lf);
      registerUser(this.lf);
      registerEnd(this.lf);
      registerPush(this.lf, this.clickPlus, this.mouseDownPlus);
      registerDownload(this.lf);
      registerPolyline(this.lf);
      registerTask(this.lf);
      this.$_render();
    },
    $_render() {
      this.lf.render(demoData);
      this.$_LfEvent();
    },
    $_getData() {
      const data = this.lf.getGraphData();
      console.log(JSON.stringify(data));
    },
    $_LfEvent() {
      this.lf.on("node:click", ({ data }) => {
        console.log("node:click", data);
        this.$data.clickNode = data;
        this.$data.dialogVisible = true;
      });
      this.lf.on("edge:click", ({ data }) => {
        console.log("edge:click", data);
        this.$data.clickNode = data;
        this.$data.dialogVisible = true;
      });
      this.lf.on("element:click", () => {
        this.hideAddPanel();
      });
      this.lf.on("edge:add", ({ data }) => {
        console.log("edge:add", data);
      });
      this.lf.on("node:mousemove", ({ data }) => {
        console.log("node:mousemove");
        this.moveData = data;
      });
      this.lf.on("blank:click", () => {
        this.hideAddPanel();
      });
      this.lf.on("connection:not-allowed", (data) => {
        this.$message.warning(data.msg);
      });
      this.lf.on("node:mousemove", () => {
        console.log("on mousemove");
      });
    },
    clickPlus(e, attributes) {
      e.stopPropagation();
      console.log("clickPlus", e, attributes);
      const { clientX, clientY } = e;
      console.log(clientX, clientY);
      this.$data.addPanelStyle.top = clientY - 40 + "px";
      this.$data.addPanelStyle.left = clientX + "px";
      this.$data.showAddPanel = true;
      this.$data.addClickNode = attributes;
    },
    mouseDownPlus(e, attributes) {
      e.stopPropagation();
      console.log("mouseDownPlus", e, attributes);
    },
    hideAddPanel() {
      this.$data.showAddPanel = false;
      this.$data.addPanelStyle.top = 0;
      this.$data.addPanelStyle.left = 0;
      this.$data.addClickNode = null;
    },
    closeDialog() {
      this.$data.dialogVisible = false;
    },
    $_catData() {
      this.$data.graphData = this.$data.lf.getGraphData();
      this.$data.dataVisible = true;
    },
  },
};
</script>
<style lang="less" scoped>
.logic-flow-view {
  height: 96vh;
  position: relative;
}
.demo-title {
  text-align: center;
  margin: 10px;
}
.demo-control {
  position: absolute;
  top: 50px;
  right: 50px;
  z-index: 2;
}
#LF-view {
  width: calc(100% - 0px);
  height: 96%;
  outline: none;
  /* margin-left: 50px; */
}
.time-plus {
  cursor: pointer;
}
.add-panel {
  position: absolute;
  z-index: 11;
  background-color: white;
  padding: 10px 5px;
}
.el-drawer__body {
  height: 80%;
  overflow: auto;
  margin-top: -30px;
  z-index: 3;
}
</style>
